package xm.com.handlertest;


public class TestThreadMsg0 {
    static String message;//全局公共变量

    static class Thread1 extends Thread{
        @Override
        public void run() {
            super.run();
            for (;;) {
                if (TestThreadMsg0.message != null){
                    System.out.println("线程:"+Thread.currentThread().getId()+"   收到:"+TestThreadMsg0.message);
                    TestThreadMsg0.message = null;
                    System.out.println("");
                    System.out.println("");
                }
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class Thread2 extends Thread{
        @Override
        public void run() {
            super.run();
            for (;;) {
                try {
                    String message="来自线程"+Thread.currentThread().getId()+"的消息";
                    System.out.println("线程:"+Thread.currentThread().getId()+"   发送:"+message);
                    TestThreadMsg0.message = message;
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args){
        Thread1 t1 = new Thread1();
        t1.start();

        Thread2 t2 = new Thread2();
        t2.start();

    }
}
